<template>
  <div>
    <input type="file" ref="fileInput" @change="handleFileChange" />
    <el-button @click="uploadData">上传数据</el-button>
  </div>
</template>

<script setup>
// import { uploadExcelApi } from "@/api/index.js";
import * as XLSX from "xlsx";

const fileInput = ref();
/**
const handleFileChange = (event) => {
  const file = event.target.files[0];
  if (file) {
    readFile(file);
  }
};
const readFile = (file) => {
  const reader = new FileReader();
  reader.onload = (e) => {
    const data = new Uint8Array(e.target.result);
    const workbook = XLSX.read(data, { type: "array" });
    const sheetName = workbook.SheetNames[0];
    const sheet = workbook.Sheets[sheetName];
    const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });
    console.log(jsonData);
  };
  reader.readAsArrayBuffer(file);
};
  */
const uploadData = () => {
  if (fileInput.value && fileInput.value.files.length > 0) {
    const file = fileInput.value.files[0];
    const formData = new FormData();
    console.log("formData", formData);
    formData.append("file", file);

    // uploadExcelApi(formData)
    //   .then((res) => {
    //     console.log("res", res);
    //   })
    //   .catch((err) => {
    //     console.log("err", err);
    //   });
  } else {
    console.warn("请选择一个文件");
  }
};
</script>
